import axios from 'axios'
import router from './router/index'
import store from './store'
import ElementUI from 'element-ui'
import * as types from './store/types'

//
//axios.defaults.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers.get['Content-Type'] = 'application/json'
//const path = "http://localhost:8080/topvision/";

axios.defaults.withCredentials = true;

const config = require('../config')
const path = config.dev.assetsUrlPath || '';

export default {

    hPost(url, params, callback, errorCallBack) {
        axios.post(path + url,
            params
        ).then((response) => {
            if (!response.data.success && response.data.msg === 'not login') {
                ElementUI.Message.error("未登录");
                setTimeout(function () {
                    store.dispatch('UserLogout');
                    router.ux_login = false;
                    router.replace("/login");
                }, 500);
                return false;
            }
            if (typeof callback === "function") {
                callback(response.data);
            }
        }).catch((error) => {
            if (error.response) {
                if (error.response.status === 500) {
                    //sever error
                    ElementUI.Notification.error({
                        title: '错误',
                        duration: 0,
                        message: '服务器异常,按F12查看详情!'
                    });
                    console.log(error.response.data.exception);
                }
            } else if (error.request) {
                console.log(error.request);
            } else {
                console.log('Error', error);
            }
            if (typeof errorCallBack === "function") {
                errorCallBack();
            }
        })
    },

    hGet(url, params, callback, errorCallBack) {
        axios.get(path + url,
            params
        ).then((response) => {
            if (!response.data.success && response.data.msg === 'not login') {
                ElementUI.Message.error("未登录");
                setTimeout(function () {
                    store.dispatch('UserLogout');
                    router.ux_login = false;
                    router.replace("/login");
                    router.push({
                        path: '/login'
                    })
                }, 500);
                return false;
            }
            if (typeof callback === "function") {
                callback(response.data);
            }
        }).catch((error) => {
            if (error.response) {
                if (error.response.status === 500) {
                    //sever error
                    ElementUI.Notification.error({
                        title: '错误',
                        duration: 0,
                        message: '服务器异常,按F12查看详情!'
                    });
                    console.log(error.response.data.exception);
                }
            } else if (error.request) {
                console.log(error.request);
            } else {
                console.log('Error', error);
            }
            if (typeof errorCallBack === "function") {
                errorCallBack();
            }
        })
    }
}
